/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Configuracion;

import Atxy2k.CustomTextField.RestrictedTextField;
import Controlador.Bean.Configuracion.Configuracion;
import Controlador.Service.Configuracion.GestionConfiguracion;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import lib.Validaciones;

/**
 *
 * @author Luis Enrique
 */
public class ConfiguracionRegistrarPanel extends javax.swing.JPanel {

    private JDialog parent;
    Configuracion c = GestionConfiguracion.getInstance().consultarConfig();
    
    //Formato decimal
    DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
    DecimalFormat df;
    DecimalFormat df2;
    
    public void setParent(JDialog parent){
        this.parent = parent;
    }
    
    public ConfiguracionRegistrarPanel() {
        initComponents();
        
        this.simbolos.setDecimalSeparator('.');
        this.df = new DecimalFormat("#0.00",simbolos);
        
        this.df2 = new DecimalFormat("#.00",simbolos);
        
        
        RestrictedTextField restricted = new RestrictedTextField(txtMaxCantPed);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtMaxCantPlan);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtPeriodoPlan);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtVencimiento);
        restricted.setOnlyNums(true);
    }

    public void llenar(){
        txtIGV.setText(df.format(c.getIgv()));
        txtTipoCambio.setText(df2.format(c.getTipoCambio()));
        txtPeriodoPlan.setText(c.getPlanVentasPeriodo()+"");
        txtMaxCantPlan.setText(c.getPlanVentasCantMax()+"");
        txtMaxCantPed.setText(c.getPedidoCantMax()+"");
        txtVencimiento.setText(c.getCotDiasVencimiento()+"");
    }
    
    public boolean valido(){
        boolean b = true;
        
        if(txtIGV.getText().isEmpty() || !Validaciones.validarReal(txtIGV.getText())){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en I.G.V.", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtTipoCambio.getText().isEmpty() || !Validaciones.validarReal(txtTipoCambio.getText())){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Tipo de Cambio", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtMaxCantPed.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Límite de Productos en Cotización", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtMaxCantPlan.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en Límite de Productos en Plan de Ventas", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtPeriodoPlan.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en el Período del Plan de Ventas", "Error en los datos", JOptionPane.OK_OPTION);
        }
        if(txtVencimiento.getText().isEmpty()){
            b = false;
            JOptionPane.showMessageDialog(this, "Error en el Período de Validez de la Nota de Crédito", "Error en los datos", JOptionPane.OK_OPTION);
        }
        return b;
    }
                
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIGV = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTipoCambio = new javax.swing.JTextField();
        txtPeriodoPlan = new javax.swing.JTextField();
        txtMaxCantPlan = new javax.swing.JTextField();
        txtMaxCantPed = new javax.swing.JTextField();
        txtVencimiento = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1784, 1549));
        setMinimumSize(new java.awt.Dimension(0, 0));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Configurables"));

        jLabel1.setText("Impuesto General a las Ventas");

        txtIGV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText("Tipo de Cambio (Dolares)");

        txtTipoCambio.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        txtPeriodoPlan.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        txtMaxCantPlan.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        txtMaxCantPed.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        txtVencimiento.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel6.setText("Período del Plan de Ventas (en días)");

        jLabel7.setText("Límite de Productos en Plan de Ventas");

        jLabel8.setText("Límite de Productos en Cotización");

        jLabel9.setText("Período de Validez de la Nota de Crédito (en días)");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel1))
                .addGap(92, 92, 92)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtVencimiento, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMaxCantPed, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMaxCantPlan, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPeriodoPlan, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTipoCambio, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(txtIGV))
                .addContainerGap(107, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTipoCambio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPeriodoPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaxCantPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaxCantPed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVencimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnRegistrar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnRegistrar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        if(valido()){
            double igv = Double.parseDouble(txtIGV.getText());
            double tipoCambio = Double.parseDouble(txtTipoCambio.getText());
            int planVentasPeriodo = Integer.parseInt(txtPeriodoPlan.getText());
            long planVentasCantMax = Long.parseLong(txtMaxCantPlan.getText());
            long pedidoCantMax = Long.parseLong(txtMaxCantPed.getText());
            int cotDiasVencimiento = Integer.parseInt(txtVencimiento.getText());
                    

            Configuracion config = new Configuracion(null,cotDiasVencimiento,igv,tipoCambio,planVentasPeriodo,pedidoCantMax,planVentasCantMax,new Date());
            GestionConfiguracion.getInstance().actualizarConfig(config);
            JOptionPane.showMessageDialog(this,"Se registró correctamente la actualización.");
            btnRegistrar.setVisible(false);
            btnRegistrar.setEnabled(false);
            btnCancelar.setText("Regresar");
        }
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        parent.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtIGV;
    private javax.swing.JTextField txtMaxCantPed;
    private javax.swing.JTextField txtMaxCantPlan;
    private javax.swing.JTextField txtPeriodoPlan;
    private javax.swing.JTextField txtTipoCambio;
    private javax.swing.JTextField txtVencimiento;
    // End of variables declaration//GEN-END:variables
}
